package cn.zn.modules.pz.cur.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.zn.framework.common.exception.RRException;
import cn.zn.modules.pz.cur.entity.TpMoney;
import cn.zn.modules.pz.cur.entity.vo.TpMoneyVO;
import cn.zn.modules.pz.cur.mapper.TpMoneyMapper;
import cn.zn.modules.pz.cur.service.TpMoneyService;
import cn.zn.modules.pz.enums.MoneyTypeEnum;
import cn.zn.modules.pz.inter.TpMoneyInter;
import cn.zn.modules.pz.inter.dto.TpMoneyDTO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zn
 */
@Service
public class TpMoneyServiceImpl extends ServiceImpl<TpMoneyMapper, TpMoney>
        implements TpMoneyService, TpMoneyInter {
    @Override
    public void save(TpMoneyDTO dto) {
        TpMoney tpMoney = new TpMoney();
        BeanUtils.copyProperties(dto, tpMoney);
        tpMoney.setId(null);
        tpMoney.setCanEdit(true);
        super.save(tpMoney);
    }

    @Override
    public void update(TpMoneyDTO dto) {
        TpMoney tpMoney = super.getOne(new QueryWrapper<TpMoney>().eq("MONEY_FREEZE_ID", dto.getMoneyFreezeId()));
        if (tpMoney == null) {
            throw new RRException("明细信息不存在");
        }
        TpMoney tpMoneySave = new TpMoney();
        BeanUtil.copyProperties(dto, tpMoneySave);
        tpMoneySave.setId(tpMoney.getId());
        super.updateById(tpMoneySave);
    }

    @Override
    public void delete(String moneyFreezeId) {
        super.remove(new QueryWrapper<TpMoney>().eq("MONEY_FREEZE_ID", moneyFreezeId));
    }

    @Override
    public List<TpMoneyVO> queryByMainId(String mainId) {
        return this.baseMapper.queryByMainId(mainId).stream().map(o->{
            o.setMoneyTypeName(MoneyTypeEnum.findByCode(o.getMoneyTypeCode()).getName());
            return o;
        }).collect(Collectors.toList());
    }
}




